package com.cpa.admin.controller;

import com.cpa.admin.config.WebSecurityConfig;
import com.cpa.admin.model.ProductDo;
import com.cpa.admin.model.UserDo;
import com.cpa.admin.service.ProductService;
import com.cpa.admin.service.UserOrderService;
import com.cpa.admin.service.UserService;
import com.cpa.admin.vo.PageVo;
import com.cpa.admin.vo.StatisticsMonthOrderVo;
import com.cpa.admin.vo.UserPageVo;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.util.Sqls;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author zhen.zhao01
 * @CreateDate 2018/11/14
 */
@Controller
@RequestMapping("user_page")
public class UserPageController {

    @Autowired
    private UserService userService;

    @Autowired
    private ProductService productService;

    @Autowired
    private UserOrderService userOrderService;


    @GetMapping
    public String userPage(HttpSession session, HttpServletRequest request, HttpServletResponse response) {

        String loginUser = getLoginUser(session);
        UserDo userDo = userService.selectOneByExample(
                Example.builder(UserDo.class)
                        .where(Sqls.custom()
                                .andEqualTo("username", loginUser)
                        ).build()
        );

        if (userDo == null) {
            response.setStatus(HttpStatus.UNAUTHORIZED.value());
            throw new RuntimeException("not login");
        }

        ProductDo productDo = productService.selectByPrimaryKey(userDo.getProductId());


        request.setAttribute("productDo",productDo);

        Integer count = userOrderService.countUserOrder(userDo.getId());
        if (count == null) {
            count = 0;
        }
        request.setAttribute("count", count);
        request.setAttribute("amount", productDo.getProductAmount().multiply(new BigDecimal(count)).setScale(2).toString());

        Integer unCleanCount = userOrderService.countUnCleanUserOrder(userDo.getId());
        if (unCleanCount == null) {
            unCleanCount = 0;
        }
        request.setAttribute("unCleanAmount", productDo.getProductAmount().multiply(new BigDecimal(unCleanCount)).setScale(2).toString());

        request.setAttribute("loginUser",loginUser);
        return "userPage";
    }


    @ResponseBody
    @GetMapping("data")
    public PageVo<UserPageVo> data(Integer page, Integer limit ,HttpSession session) {

        UserDo userDo = userService.selectOneByExample(
                Example.builder(UserDo.class)
                        .where(Sqls.custom()
                                .andEqualTo("username", getLoginUser(session))
                        ).build()
        );

        Page<UserPageVo> productVoPage = PageHelper.startPage(page, limit)
                .doSelectPage(() -> userOrderService.findUserPageVoList(userDo.getId()));


        PageVo<UserPageVo> pageVo = new PageVo<>();
        pageVo.setCode(0);
        pageVo.setCount(productVoPage.getTotal());
        pageVo.setData(productVoPage.getResult());

        return pageVo;

    }

    @ResponseBody
    @GetMapping("statistics")
    public Map<String, List> statistics(HttpSession session) {
        List<StatisticsMonthOrderVo> statisticsMonthOrderVoList = userOrderService
                .statisticsUserMonthOrder(userService.getId(getLoginUser(session)));
        List<String> days = new ArrayList<>(statisticsMonthOrderVoList.size());
        List<Integer> count = new ArrayList<>(statisticsMonthOrderVoList.size());

        statisticsMonthOrderVoList.stream().forEach(s -> {
            days.add(s.getDay());
            count.add(s.getCount());
        });

        Map<String, List> map = new HashMap<>();
        map.put("days", days);
        map.put("count", count);

        return map;
    }



    private String getLoginUser(HttpSession session) {
        return (String) session.getAttribute(WebSecurityConfig.SESSION_KEY);
    }




}
